package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Exercise;
import com.itheima.pojo.Food;
import com.itheima.service.ExerciseService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/exercise")
@RestController
public class ExerciseController1 {

    @Reference
    private ExerciseService exerciseService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){

        //读取Excel文件数据
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list!=null&&list.size()>0){
                List<Exercise> exerciseList=new ArrayList<>();
                for (String[] strings : list) {
                    String bid = strings[0];
                    String name = strings[1];
                    String time = strings[2];
                    String frequency = strings[3];
                    String minage = strings[4];
                    String maxage = strings[5];
                    Exercise exercise = new Exercise(bid,name,time,frequency,minage,maxage);
                    exerciseList.add(exercise);
                }
                exerciseService.add(exerciseList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_EXERCISE_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_EXERCISE_SUCCESS);

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult result = exerciseService.pageQuery(queryPageBean);
        return result;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Exercise exercise){
        try {
            exerciseService.addExercise(exercise);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, "运动添加失败");
        }
        return new Result(true,"运动添加成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer sid){
        try {
            Exercise exercise = exerciseService.findById(sid);
            return new Result(true,"查询运动成功",exercise);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false,"查询运动失败");
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Exercise exercise){
        try {
            exerciseService.edit(exercise);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false,"膳食运动失败");
        }

        return new Result(true,"膳食运动成功");
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer sid){
        try {
            exerciseService.deleteById(sid);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, "膳食删除失败");
        }
        return new Result(true,"膳食删除成功");
    }

}
